$(function () {
    $("#jqGrid").jqGrid({
        url: apiURL + 'cm/dictionary/list',
        datatype: "json",
        colModel: [			
			{ label: 'dictionaryId', name: 'dictionaryId', index: 'dictionary_id', width: 50, key: true ,hidden:true},
			{ label: '类别', name: 'type', index: 'type', width: 80 }, 			
			{ label: '名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '代码', name: 'code', index: 'code', width: 80 }, 			
			{ label: '值', name: 'value', index: 'value', width: 80 }, 			
			{ label: '序号', name: 'seqNo', index: 'seq_no', width: 80 }, 			
			{ label: '状态', name: 'status', index: 'status', width: 80 }		
        ],
		viewrecords: true,
        height: Commons.getGridHeight("jqGrid"),
        rowNum: 30,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{type:null},
		showList: true,
		title: null,
		updateFlag:false,
		dictionary: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.updateFlag=false;
			vm.title = "新增";
			vm.dictionary = {};
		},
		update: function (event) {
			var dictionaryId = getSelectedRow();
			if(dictionaryId == null){
				return ;
			}
			vm.showList = false;
			vm.updateFlag=true;
            vm.title = "修改";
            
            vm.getInfo(dictionaryId)
		},
		saveOrUpdate: function (event) {
			var url = vm.dictionary.dictionaryId == null ? "cm/dictionary/save" : "cm/dictionary/update";
			$.ajax({
				type: "POST",
			    url: apiURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dictionary),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var dictionaryIds = getSelectedRows();
			if(dictionaryIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: apiURL + "cm/dictionary/delete",
                    contentType: "application/json",
				    data: JSON.stringify(dictionaryIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(dictionaryId){
			$.get(apiURL + "cm/dictionary/info/"+dictionaryId, function(r){
                vm.dictionary = r.dictionary;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			
			//查询前清空参数的缓存
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            var params = getQueryParams(vm.q);
            $.each(postData, function (k, v) {
            	if(!params[k]){
            		delete postData[k];
            	}
            });
            
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:params,
                page:page
            }).trigger("reloadGrid");
		}
	}
});